package com.example.weather_app.db;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;
import java.util.List;

public class DBManager {
    public static SQLiteDatabase database;
    private String city1;

    /**
     * 初始化数据库
     * @param context
     */
    public static void initDB(Context context){
        DBHelper dbHelper = new DBHelper(context);
        database = dbHelper.getWritableDatabase();
    }

    /**
     * 查询所有城市并返回
     * @return
     */
    public static List<String> queryAllCityName(){
        Cursor cursor = database.query("info", null, null, null, null, null, null);
        List<String> cityList = new ArrayList<>();
        while (cursor.moveToNext()){
            String city = cursor.getString(cursor.getColumnIndex("city"));
            cityList.add(city);
        }
        return cityList;
    }

    /**
     * 修改城市信息
     * @param city
     * @param content
     * @return
     */
    public static int updataInfoByCity(String city,String content){
        ContentValues values = new ContentValues();
        values.put("content",content);
        return database.update("info",values,"city=?",new String[]{city});
    }

    /**
     * 添加城市
     * @param city
     * @param content
     * @return
     */
    public static long addCityInfo(String city,String content){
        ContentValues values = new ContentValues();
        values.put("city",city);
        values.put("content",content);
        return database.insert("info",null,values);
    }

    /**
     * 根据城市名获取城市信息
     * @param city
     * @return
     */
    public static String queryInfoByCity(String city){
        Cursor cursor = database.query("info", null, null, null, null, null, null);
        if (cursor.getCount()>0) {
            cursor.moveToFirst();
            String content = cursor.getString(cursor.getColumnIndex("content"));
            return content;
        }
        return null;
    }

    /**
     * 获取城市数量
     * @return
     */
    public static int getCityCount(){
        Cursor cursor = database.query("info", null, null, null, null, null, null);
        return cursor.getCount();
    }

    public static List<DatabaseBean> queryAllInfo(){
        List<DatabaseBean> databaseBeanList = new ArrayList<>();
        Cursor cursor = database.query("info", null, null, null, null, null, null);
        while (cursor.moveToNext()){
            int id = cursor.getInt(cursor.getColumnIndex("_id"));
            String city = cursor.getString(cursor.getColumnIndex("city"));
            String content = cursor.getString(cursor.getColumnIndex("content"));
            DatabaseBean bean = new DatabaseBean(id,city,content);
            databaseBeanList.add(bean);
        }
        return databaseBeanList;
    }

    public static int deleteInfoByCity(String city){
        return database.delete("info", "city=?", new String[]{city});
    }

    //删除所有数据
    public static void deleteAllInfo(){
        String sql = "delete from info";
        database.execSQL(sql);
    }
}
